import { Injectable } from '@angular/core';
import { CanActivate, CanActivateChild, CanDeactivate, CanLoad, Route, UrlSegment, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { MemberVo } from '../model/member-vo';
import { MemberStorageService } from '../services/member-storage.service';

@Injectable({
  providedIn: 'root'
})
export class MemberAuthorizeGuard implements CanActivate {

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
      const curMember:MemberVo|null = this.memberStorage.get();
      if(curMember && curMember.id > 0){
        return true;
      }
      this.router.navigate(['/member/login'], { queryParams: { redirect: state.url }});
      return false;
  }

  constructor(private router:Router, private memberStorage: MemberStorageService){}
}
